package com.hippoframework.bean.processor;
import com.hippoframework.bean.ApplicationContext;
import com.hippoframework.bean.aware.ApplicationContextAware;
import com.hippoframework.bean.aware.EnvironmentAware;

/**
 * @author TylerW
 * @date 4/15/2020 4:39 PM
 */
public class ApplicationContextAwareProcessor implements BeanPostProcessor {

    private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void doProcessBeforeInit(Object bean, String beanName) {
        if ((bean instanceof ApplicationContextAware)) {
            ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
        }
        if ((bean instanceof EnvironmentAware)) {
            ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
        }
    }
}
